package com.marvinlabs.widget.slideshow.transition;

import com.marvinlabs.widget.slideshow.SlideShowView;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.animation.timelinecurves.SpringCurve;
import ohos.agp.components.Component;

/**
 * A transition maker to flip the slides like cards.
 * <p/>
 * Created by Vincent Mimoun-Prat @ MarvinLabs on 02/06/2014.
 */
public class FlipTransitionFactory extends BaseTransitionFactory {

    public enum FlipAxis {
        HORIZONTAL, VERTICAL;
    }

    private FlipAxis axis;

    //==============================================================================================
    // GENERAL METHODS
    //==

    /**
     * Default constuctor
     */
    public FlipTransitionFactory() {
        this(DEFAULT_DURATION, new SpringCurve(), FlipAxis.VERTICAL);
    }

    /**
     * Constructor
     *
     * @param duration Duration for the transition in ms
     */
    public FlipTransitionFactory(long duration) {
        this(duration, new SpringCurve(), FlipAxis.VERTICAL);
    }

    /**
     * Constructor
     *
     * @param duration The duration for the transition in ms
     * @param interpolator The kind of interpolator we need
     */
    public FlipTransitionFactory(long duration, Animator.TimelineCurve interpolator) {
        this(duration, interpolator, FlipAxis.VERTICAL);
    }

    /**
     * Constructor
     *
     * @param duration The duration for the transition in ms
     * @param axis The axis on which we want to flip
     */
    public FlipTransitionFactory(long duration, FlipAxis axis) {
        this(duration, new SpringCurve(), axis);
    }

    /**
     * Constructor
     *
     * @param duration The duration for the transition in ms
     * @param interpolator The kind of interpolator we need
     * @param axis
     */
    public FlipTransitionFactory(long duration, Animator.TimelineCurve interpolator, FlipAxis axis) {
        super(duration, interpolator);
        this.axis = axis;
    }

    /**
     * Get the flip axis
     *
     * @return The flip axis
     */
    public FlipAxis getAxis() {
        return axis;
    }

    /**
     * Set the flip axis
     *
     * @param axis the flip axis
     */
    public void setDirection(FlipAxis axis) {
        this.axis = axis;
    }

    //==============================================================================================
    // INTERFACE IMPLEMENTATION: SlideTransitionFactory
    //==

    @Override
    public AnimatorValue getInAnimator(Component target, SlideShowView parent, int fromSlide, int toSlide) {
        target.setAlpha(1);
        target.setScaleX(1);
        target.setScaleY(1);
        target.setTranslationX(0);
        target.setTranslationY(0);

        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(getDuration() / 2);
        animator.setDelay(getDuration() / 2);
        animator.setCurve(getInterpolator());
        animator.setValueUpdateListener((animatorValue, value) -> {
            target.setRotation(-90 * value);
        });

        return animator;
    }

    @Override
    public AnimatorValue getOutAnimator(Component target, SlideShowView parent, int fromSlide, int toSlide) {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(getDuration() / 2);
        animator.setCurve(getInterpolator());
        animator.setValueUpdateListener((animatorValue, value) -> {
            target.setRotation(90 * value - 90);
        });

        return animator;
    }

}
